<?php

/**
 * This is the model class for table "product".
 *
 * The followings are the available columns in table 'product':
 * @property integer $id
 * @property string $name
 * @property string $teaser
 * @property string $description
 * @property string $image
 * @property string $price
 * @property string $price_km
 * @property integer $hot
 * @property integer $recent
 * @property integer $status
 * @property integer $category
 * @property string $created_on
 * @property string $modified_on
 */
class Product extends CActiveRecord
{
	const STATUS_ACTIVED = 1;
	const STATUS_INIT = 0;

	const HOT_ACTIVED = 1;
	const HOT_INIT = 0;

	const RECENT_ACTIVED = 1;
	const RECENT_INIT = 0;

	const TYPE_GAO_DAC_SAN = 'GAO_DAC_SAN';
	const TYPE_GAO_DEO = 'GAO_DEO';
	const TYPE_GAO_BINH_DAN = 'GAO_BINH_DAN';
	const TYPE_GAO_KHO = 'GAO_KHO';

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'product';
	}

	public function beforeSave()
	{
		if ($this->isNewRecord)
			$this->created_on = new CDbExpression('NOW()');

		return parent::beforeSave();
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, price', 'required'),
			array('hot, recent, status', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>255),
			array('image, teaser', 'length', 'max'=>500),
			array('price, price_km', 'length', 'max'=>10),
			array('created_on', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, teaser, description, image, price, price_km, hot, recent, status, category, created_on, modified_on', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Tên sản phẩm',
			'image' => 'Ảnh',
			'teaser' => 'Mô tả ngắn',
			'description' => 'Mô tả sản phẩm',
			'price' => 'Giá tiền / Kg',
			'price_km' => 'Giá khuyến mại',
			'hot' => 'Là sản phẩm đặc biệt',
			'recent' => 'Là sản phẩm khuyên dùng',
			'status' => 'Trạng thái',
			'category' => 'Danh mục',
			'created_on' => 'Ngày tạo',
			'modified_on' => 'Ngày cập nhật',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('teaser',$this->teaser,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('price',$this->price,true);
		$criteria->compare('price_km',$this->price_km,true);
		$criteria->compare('hot',$this->hot);
		$criteria->compare('recent',$this->recent);
		$criteria->compare('status',$this->status);
		$criteria->compare('category',$this->category);
		$criteria->compare('created_on',$this->created_on,true);
		$criteria->compare('modified_on',$this->modified_on,true);
		$criteria->order = ' id desc';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	function getCategories()
	{
        return array(
        	self::TYPE_GAO_BINH_DAN => 'Gạo bình dân',
        	self::TYPE_GAO_DAC_SAN => 'Gạo đặc sản',
        	self::TYPE_GAO_DEO => 'Gạo dẻo',
        	self::TYPE_GAO_KHO => 'Gạo khô',
        );
	}
}